/*
 * Isman Usoh<isman.usoh@gmail.com>
 * www.facebook.com/isman.usoh
 */
package com.isman.postcodeinfo.database;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class PostCodeDatabaseHelper extends SQLiteOpenHelper {
	
	public static final String TABLE_POSTCODE = "postcode";
	public static final String POSTCODE_COLUMN_ID = "_id";
	public static final String POSTCODE_COLUMN_ADDRESS = "address";
	public static final String POSTCODE_COLUMN_DISTRICT  = "district";
	public static final String POSTCODE_COLUMN_PROVINCE = "province";
	public static final String POSTCODE_COLUMN_POSTCODE = "postcode";
	public static final String POSTCODE_COLUMN_NAME = "name";
	public static final String POSTCODE_COLUMN_LONG = "long";
	public static final String POSTCODE_COLUMN_LAT = "lat";
	
	
	private static final String DATABASE_NAME = "postcode.db";
	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_PATH = "/data/data/com.isman.postcodeinfo/databases/";
	private static final String DATABASE_FILE = DATABASE_PATH + DATABASE_NAME;

	private Context mContext;
	public PostCodeDatabaseHelper(Context mContext) {
		super(mContext, DATABASE_NAME, null, DATABASE_VERSION);
		this.mContext = mContext;
		
		copyDataBase();
	}
	
	public boolean checkDataBase(){
		Log.i("Isman", "Check Database");
	    SQLiteDatabase checkDB = null;
	    try{
	        checkDB = SQLiteDatabase.openDatabase(DATABASE_FILE, null, SQLiteDatabase.OPEN_READONLY | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
	    }catch(SQLiteException e){
	    	Log.i("Isman", "Create Database");
	    	copyDataBase();
	    }
	    if(checkDB != null){
	        checkDB.close();
	    }
	    return checkDB != null ? true : false;
	}

	@Override
	public void onCreate(SQLiteDatabase database) {
		Log.i("Isman", "onCreate");
//		copyDataBase();
//		String template = "CREATE TABLE %s (%s integer primary key autoincrement, %s TEXT, %s TEXT NOT NULL, %s TEXT NOT NULL, %s TEXT NOT NULL);";
//
//		String queryString = String.format(
//				template,
//				TABLE_POSTCODE,
//				POSTCODE_COLUMN_ID,
//				POSTCODE_COLUMN_ADDRESS,
//				POSTCODE_COLUMN_DISTRICT,
//				POSTCODE_COLUMN_PROVINCE,
//				POSTCODE_COLUMN_POSTCODE);
//		database.execSQL(queryString);
//		
//		template = "CREATE TABLE %s (%s INTEGER primary key autoincrement, %s INTEGER NOT NULL, %s TEXT NOT NULL, %s NUMERIC NOT NULL, %s NUMERIC NOT NULL);";
//		queryString = String.format(
//				template,
//				TABLE_POSTOFFICE,
//				POSTOFFICE_COLUMN_ID,
//				POSTOFFICE_COLUMN_POSTCODE_ID,
//				POSTOFFICE_COLUMN_NAME,
//				POSTOFFICE_COLUMN_LAT,
//				POSTOFFICE_COLUMN_LONG);
//		database.execSQL(queryString);
	}

	@Override
	public void onUpgrade(SQLiteDatabase database, int arg1, int arg2) {
		Log.i("Isman", "onUpgrade");
//		database.execSQL("DROP TABLE IF EXISTS " + TABLE_POSTOFFICE);
//		database.execSQL("DROP TABLE IF EXISTS " + TABLE_POSTCODE);
	    this.onCreate(database);
	}

	private void copyDataBase(){
		Log.i("Isman", "Copy Database");
	    InputStream myInput;
		try {
			
			File file=new File(DATABASE_FILE);
			if(file.exists()){
				file.deleteOnExit();
			}

			myInput = mContext.getAssets().open(DATABASE_NAME);
		    OutputStream myOutput = new FileOutputStream(DATABASE_FILE);


		    byte[] buffer = new byte[1024];
		    int length;
		    while ((length = myInput.read(buffer))>0){
		        myOutput.write(buffer, 0, length);
		    }
		    
		    myOutput.flush();
		    myOutput.close();
		    myInput.close();
		} catch (IOException e) {
			Log.e("Isman", e.getMessage());
		}
	}
}
